/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.nicmodule.core;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.utils.StringVerification;

/**Implementor interface side of bridge pattern
 *
 * @author alperb
 */
public abstract class NetworkOS {

    public abstract boolean enableInterface(String iPath,
            boolean enabled);

    public abstract boolean setInterfaceConfiguration(String iPath,
            String ip,
            String gateWay,
            String dns,
            String subnetMask,
            Boolean fromDhcp,
            String messageId);

    public abstract boolean renewDHCPClient(String iPath);

    public abstract String getIp(String iPath);

    public abstract String getDns(String iPath);

    public abstract String getGateway(String iPath);

    public abstract String getSubnetMask(String iPath);

    public abstract String getMacAddress(String iPath);

    public abstract boolean isEnabled(String iPath);

    /**
     * verification interface parameters implementation method
     * throws exception if interface is null
     * sequentially calls all parameters verify methods
     * @param all needed parameters in String form
     * @return true/false to represent succes or fail
     */
    public boolean verifyInterface(String iPath,
            String ip,
            String gateWay,
            String dns,
            String subnetMask,
            String messageId) {

        StringBuilder log = new StringBuilder();
        boolean isIp = false;
        boolean isGateway = false;
        boolean isDns = false;
        boolean isSubnetMask = false;
        try {

            if ((iPath == null) || (iPath.trim().equals(""))) {
                throw new NicModuleException(
                        NicModuleExceptionType.IFACENAMENULL,
                        "NetworkOS:verifyInterface called with null as interface IP:"
                        + ip + " GATEWAY:" + gateWay
                        + " DNS:" + dns
                        + " SUBNETMASK:" + subnetMask);
            }

            if (ip != null && !ip.trim().equals("")) {
                String curIp = null;
                while (!isIp) {
                    curIp = getIp(iPath);
                    if (curIp != null) {
                        isIp = StringVerification.verifyText(ip,
                                curIp);
                    }
                    if(!curIp.startsWith("169")&& !curIp.equals("0.0.0.0")&& !curIp.isEmpty()){
                        break;
                    }
                    Thread.sleep(1000);
                }
                log.append("\nCurrent ip is " + curIp
                        + " comparing with " + ip
                        + ". Ip address is " + (isIp ? "verified" : " not verified"));
            } else {
                isIp = true;
            }
            if (gateWay != null && !gateWay.trim().equals("")) {

                String curGateway = getGateway(iPath);
                isGateway = StringVerification.verifyText(gateWay,
                        curGateway);
                log.append("\nCurrent gateway address is " + curGateway
                        + " comparing with " + gateWay
                        + ". Gateway address is " + (isGateway ? "verified" : " not verified"));
            } else {
                isGateway = true;
            }
            if (dns != null && !dns.trim().equals("")) {
                String curDns = getDns(iPath);
                isDns = StringVerification.verifyText(dns,
                        curDns);
                log.append("\nCurrent dns address is " + curDns
                        + " comparing with " + dns
                        + ". Dns address is " + (isDns ? "verified" : " not verified"));
            } else {
                isDns = true;
            }
            if (subnetMask != null && !subnetMask.trim().equals("")) {
                String curSubnetMask = getSubnetMask(iPath);
                isSubnetMask = StringVerification.verifyText(subnetMask,
                        curSubnetMask);
                log.append("\nCurrent subnetMask address is " + curSubnetMask
                        + " comparing with " + subnetMask
                        + ". SubnetMask address is " + (isSubnetMask ? "verified" : " not verified"));
            } else {
                isSubnetMask = true;
            }
            LogSenderService.putMessageOnQueue(
                    "NicModuleVerifyInterfaceConfigurationAction",
                    AtofLogLevel.INFO,
                    log.toString(),
                    messageId);
            return (isIp & isGateway & isDns & isSubnetMask);
        } catch (Exception ex) {
            throw new NicModuleException(NicModuleExceptionType.UNEXPLAINED,
                    "NetworkOS:verifyInterface exception "
                    + ex.getMessage().toString());
        }
    }

     public abstract boolean isInterfaceExists(String iPath) ;
}
